#include "Thread.hpp"
#include "lockGuard.hpp"
#include <unistd.h>
#include <string>

int tickets = 100;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void threadRoutine(void* args)
{
    std::string message = static_cast<const char*>(args);
    while (true)
    {
        LockGuard lockguard(&mutex);
        if (tickets > 0)
        {
            std::cout << message << " get a ticket: " << tickets-- << std::endl;
            usleep(2000);
        }
        else
        {
            break;
        }
    }
}

int main()
{
    Thread t1(1, threadRoutine, (void*)"hello world1");
    Thread t2(2, threadRoutine, (void*)"hello world2");
    Thread t3(3, threadRoutine, (void*)"hello world3");
    Thread t4(4, threadRoutine, (void*)"hello world4");
    //std::cout << t.name() << " " << t.threadid() << " " << t.status() << std::endl;
    //std::cout << t.name() << " " << t.threadid() << " " << t.status() << std::endl;
    t1.run();
    t2.run();
    t3.run();
    t4.run();

    t1.join();
    t2.join();
    t3.join();
    t4.join();
    return 0;
}